package com.schoolvisitors.response;

import java.io.Serial;
import java.util.HashMap;

public class ResultBody extends HashMap<String, Object> {

    @Serial
    private static final long serialVersionUID = 1L;
    public static final String STATUS_TAG = "status";
    public static final String CODE_TAG = "code";
    public static final String MSG_TAG = "msg";
    public static final String DATA_TAG = "data";

    public ResultBody(boolean status, Integer code, String msg, Object data) {
        this.put(CODE_TAG, code);
        this.put(MSG_TAG, msg);
        this.put(STATUS_TAG, status);
        if (null != data) {
            this.put(DATA_TAG, data);
        }
    }
    public static ResultBody body(boolean status, Integer code, String msg) {
        return new ResultBody(status, code, msg, null);
    }

    public static ResultBody success(){
        return new ResultBody(true, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
    }
    public static ResultBody success(String msg){
        return new ResultBody(true, ResultCode.SUCCESS.getCode(), msg, null);
    }
    public static ResultBody success(Object data){
        return new ResultBody(true, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }
    public static ResultBody fail(String message){
        return new ResultBody(false, 400, message, null);
    }
    public static ResultBody fail(ResultCode resultCode){
        return new ResultBody(false, resultCode.getCode(), resultCode.getMessage(), null);
    }
    public static ResultBody fail(ResultCode resultCode, Object data){
        return new ResultBody(false, resultCode.getCode(), resultCode.getMessage(), data);
    }
    public static ResultBody error(){
        return new ResultBody(false, ResultCode.UNKNOWN_ERROR.getCode(), ResultCode.UNKNOWN_ERROR.getMessage(), null);
    }
    public static ResultBody error(ResultCode resultCode){
        return new ResultBody(false, resultCode.getCode(), resultCode.getMessage(), null);
    }
    public static ResultBody error(ResultCode resultCode, Object data){
        return new ResultBody(false, resultCode.getCode(), resultCode.getMessage(), data);
    }
}

